package com.tianlh.service.serializable;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * @author wb-tianlihui
 * @date 2017/12/20
 */
public class SerializationUtils {

    private static final String PATH = "D:\\Serialization.txt";

    public static void writeObject(Serializable s) {
        if (null == s) {
            return;
        }
        ObjectOutputStream out = null;
        try {
            out = new ObjectOutputStream(new FileOutputStream(PATH));
            out.writeObject(s);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static Object readObject() {
        ObjectInputStream in = null;
        Object var = null;
        try {
            in = new ObjectInputStream(new FileInputStream(PATH));
            var = in.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return var;
        }
    }
}
